package com.BMS.DAOImpl;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.BMS.DAO.ReaderDao;
import com.BMS.entity.Reader;

/**
 * 读者实体类DAO层接口实现类
 * @author lenovo
 *
 */

@Repository("ReaderDaoImpl")
public class ReaderDaoImpl implements ReaderDao {

	@Resource
	private SessionFactory sessionFactory;
	
	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	// add
	@Override
	public void addReader(Reader reader) {
		this.getSession().save(reader);
	}

	// delete
	@Override
	public void deleteReaderById(String reader_id) {
		this.getSession().createQuery("delete Reader where reader_ID=?").setParameter(0, reader_id).executeUpdate();
	}

	// find
	@Override
	public Reader findReaderById(String reader_id) {
		return (Reader)this.getSession().createQuery("from Reader where reader_ID=?").setParameter(0, reader_id).uniqueResult();
	}

	// update
	@Override
	public void updateReader(Reader reader) {
		this.getSession().update(reader);
	}

}
